package com.niannian.activity;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import cn.jpush.android.api.InstrumentedActivity;
import cn.jpush.android.api.JPushInterface;
import com.niannian.view.CircleImageView;
import com.niannian.db.MyDBUser;
import com.niannian.util.Common;
import com.niannian.util.SpocketHelper;
import com.niannian.util.Sys;
import com.niannian.util.UmengPage;
import com.niannian.util.UserInfoManager;
import com.niannian.R;
import com.umeng.analytics.MobclickAgent;

public class Welcome extends InstrumentedActivity {
	private SharedPreferences sp;
	private ViewPager viewpage;
	private ArrayList<View> viewlist;
	private ImageView img;
	private LinearLayout iv_logo;
	public static boolean started = false;
	RelativeLayout rl_load;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
		started = true;

		sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
		iv_logo = (LinearLayout) findViewById(R.id.load_iv);
		// iv_logo.setBackgroundResource(R.drawable.welcome);
		AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 1.0f);
		alphaAnimation.setDuration(3000);
		iv_logo.setAnimation(alphaAnimation);
		rl_load = (RelativeLayout) findViewById(R.id.rl_load);
		rl_load.setVisibility(View.GONE);
		alphaAnimation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation arg0) {
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
			}

			@Override
			public void onAnimationEnd(Animation arg0) {
				// boolean first =
				// sp.getBoolean(SpocketHelper.getPackageVersion(Welcome.this),
				// true);
				// if (first) {
				// rl_load.setVisibility(View.VISIBLE);
				// showloading();
				// } else {
				// doback();
				// }

				doback();

			}
		});

		// getDeviceInfo(this);
	}

	@Override
	public void onStart() {
		super.onStart();
		// addShortCut();
	}

	private void doback() {
		try {
			checkFillInfo(Welcome.this);
		} catch (Exception e) {
			startActivity(new Intent(Welcome.this, LoginBeforeActivity.class));
		} finally {
			finish();
		}
	}

	CircleImageView riv_load1, riv_load2, riv_load3, riv_load4, riv_loading;

	private void showloading() {

		riv_load1 = (CircleImageView) findViewById(R.id.riv_load1);
		riv_load2 = (CircleImageView) findViewById(R.id.riv_load2);
		riv_load3 = (CircleImageView) findViewById(R.id.riv_load3);
		riv_load4 = (CircleImageView) findViewById(R.id.riv_load4);
		riv_loading = (CircleImageView) findViewById(R.id.riv_loading);

		iv_logo.setVisibility(View.GONE);
		viewpage = (ViewPager) findViewById(R.id.viewpager);
		// group = (ViewGroup) findViewById(R.id.viewGroup);
		viewlist = new ArrayList<View>();
		LayoutInflater mli = LayoutInflater.from(this);
		View vi0 = mli.inflate(R.layout.loading0, null);
		View vi1 = mli.inflate(R.layout.loading1, null);
		View vi2 = mli.inflate(R.layout.loading2, null);
		View vi3 = mli.inflate(R.layout.loading3, null);
		View vi4 = mli.inflate(R.layout.loading4, null);
		viewlist.add(vi0);
		viewlist.add(vi1);
		viewlist.add(vi2);
		viewlist.add(vi3);
		viewlist.add(vi4);
		PagerAdapter pageada = new PagerAdapter() {

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}

			@Override
			public int getCount() {
				return viewlist.size();
			}

			@Override
			public void destroyItem(View container, int position, Object object) {
				((ViewPager) container).removeView(viewlist.get(position));
			}

			@Override
			public CharSequence getPageTitle(int position) {
				return null;
			}

			@Override
			public Object instantiateItem(View container, int position) {
				((ViewPager) container).addView(viewlist.get(position));
				if (position == viewlist.size() - 1) {
					Button in = (Button) findViewById(R.id.btn_welcome_in);

					in.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							Editor edit = sp.edit();
							edit.putBoolean(SpocketHelper.getPackageVersion(Welcome.this), false);
							edit.commit();
							doback();
						}
					});

				}
				return viewlist.get(position);
			}
		};
		viewpage.setAdapter(pageada);
		viewpage.setOnPageChangeListener(new mListener());
	}

	class mListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int arg0) {

			anim(arg0);
		}
	}

	int forep = 0;

	private void anim(int position) {
		if (position > forep && position == 4) {
			rl_load.setVisibility(View.GONE);
		} else {
			if (forep == 4) {
				rl_load.setVisibility(View.VISIBLE);
			} else {
				int one = Sys.Dp2Px(Welcome.this, 26);
				int fromXDelta = 0;
				int toXDelta = 0;
				Animation animation = null;
				if (position > forep) {
					fromXDelta = forep * one;
					toXDelta = fromXDelta + one;
				} else {
					fromXDelta = forep * one;
					toXDelta = fromXDelta - one;
				}
				animation = new TranslateAnimation(fromXDelta, toXDelta, 0, 0);
				if (animation != null) {
					animation.setFillAfter(true);
					animation.setDuration(300);
					riv_loading.startAnimation(animation);
				}
			}
		}

		forep = position;
	}

	public void addShortCut() {
	}

	public void checkFillInfo(Activity curActivity) {
		MyDBUser userInfoManager = UserInfoManager.getUserInfoInstance();
		try {
			userInfoManager.loadFromPref();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (userInfoManager != null && !userInfoManager.isDataInvalid()) {
			if (userInfoManager.info_completed == 1) {
				if (Common.empty(userInfoManager.token2)) {
					startActivity(new Intent(curActivity, LoginBeforeActivity.class));
				} else {
					if (userInfoManager.family == 0) {
						startActivity(new Intent(curActivity, CreateFamilyActivity.class));
					} else {
						startActivity(new Intent(curActivity, HomeActivity.class));
					}
				}
			} else {
				startActivity(new Intent(curActivity, RegisterThereActivity.class));
			}
		} else {
			startActivity(new Intent(curActivity, LoginBeforeActivity.class));
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		JPushInterface.onResume(this);
		MobclickAgent.onPageStart(UmengPage.W_LOGO);
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(UmengPage.W_LOGO);
		MobclickAgent.onPause(this);
		JPushInterface.onPause(this);
	}

	public static String getDeviceInfo(Context context) {
		try {
			org.json.JSONObject json = new org.json.JSONObject();
			android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

			String device_id = tm.getDeviceId();

			android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context.getSystemService(Context.WIFI_SERVICE);

			String mac = wifi.getConnectionInfo().getMacAddress();
			json.put("mac", mac);

			if (TextUtils.isEmpty(device_id)) {
				device_id = mac;
			}

			if (TextUtils.isEmpty(device_id)) {
				device_id = android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
			}

			json.put("device_id", device_id);

			return json.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
